Contact me| Data Analyst Training Program @ DSP
# Settings
library(ggplot2); library(reshape2); library(psych); library(dplyr)
##
## Attaching package: 'psych'
##
## The following object is masked from 'package:ggplot2':
##
## %+%
##
##
## Attaching package: 'dplyr'
##
## The following object is masked from 'package:stats':
##
## filter
##
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
# Get Working Directory
getwd()
## [1] "/Users/Rafe/Dropbox/~OnGoing/20150131 A1"
# Windows
# setwd("C:/Users/ABACUS/Desktop/Dropbox/~OnGoing/20150131 A1")
# MAC
setwd("~Rafe/Dropbox/~OnGoing/20150131 A1")
# List files
# list.files()
# Read
# Windows
# bike <- read.csv(file="ubike-big5.csv", header = TRUE)
# MAC
bike <- read.csv(file="ubike-utf8.csv", header = TRUE)
bikeori <- bike
bike <- bikeori
HELP
MORE TIPS
# 筆記法# adjust console output width
options(width=100)
# Dimensions
dim(bike)
## [1] 202014 16
names(bike)
## [1] "日期" "時間" "場站代號" "場站區域" "場站名稱" "經度"
## [7] "緯度" "總停車格" "平均車輛數" "最大車輛數" "最小車輛數" "車輛數標準差"
## [13] "平均空位數" "最大空位數" "最小空位數" "空位數標準差"
# Glance
head(bike)
## 日期 時間 場站代號 場站區域 場站名稱 經度 緯度 總停車格 平均車輛數
## 1 2014-12-08 15 2 大安區 捷運國父紀念館站(2號出口) 25.04100 121.5569 48 24.000
## 2 2014-12-08 15 3 信義區 台北市政府 25.03780 121.5652 40 10.333
## 3 2014-12-08 15 4 信義區 市民廣場 25.03604 121.5623 60 39.333
## 4 2014-12-08 15 5 信義區 興雅國中 25.03656 121.5687 60 34.167
## 5 2014-12-08 15 6 信義區 世貿二館 25.03474 121.5657 80 31.333
## 6 2014-12-08 15 7 信義區 信義廣場(台北101) 25.03304 121.5656 80 23.571
## 最大車輛數 最小車輛數 車輛數標準差 平均空位數 最大空位數 最小空位數 空位數標準差
## 1 27 23 1.549 22.000 23 19 2
## 2 13 8 1.862 29.667 32 27 2
## 3 40 38 1.033 20.667 22 20 1
## 4 35 33 0.753 25.833 27 25 1
## 5 32 31 0.516 47.667 48 47 1
## 6 25 23 0.787 56.429 57 55 1
tail(bike, 10)
## 日期 時間 場站代號 場站區域 場站名稱 經度 緯度 總停車格
## 202005 2015-01-12 12 1060 汐止區 汐止區綜合運動場 25.06624 121.6591 44
## 202006 2015-01-12 12 1061 汐止區 汐止公園 25.06768 121.6598 52
## 202007 2015-01-12 12 1064 新店區 中正國小 24.97150 121.5363 32
## 202008 2015-01-12 12 1065 三重區 開元市民活動中心 25.06607 121.5036 42
## 202009 2015-01-12 12 1066 汐止區 雍和科學園區 25.05808 121.6260 44
## 202010 2015-01-12 12 1068 三重區 二重國中 25.06596 121.4805 70
## 202011 2015-01-12 12 1070 三重區 新北市立圖書館三重分館 25.06610 121.4955 50
## 202012 2015-01-12 12 1071 三重區 三張公園 25.07051 121.4909 44
## 202013 2015-01-12 12 70 信義區 捷運台北101/世貿站 25.03275 121.5616 52
## 202014 2015-01-12 12 121 中山區 龍江南京路口 25.05298 121.5406 66
## 平均車輛數 最大車輛數 最小車輛數 車輛數標準差 平均空位數 最大空位數 最小空位數 空位數標準差
## 202005 26.086 27 26 0.284 16.914 17 16 0
## 202006 18.686 23 17 1.811 32.314 34 28 2
## 202007 15.429 18 14 1.220 15.571 17 13 1
## 202008 15.457 17 14 0.657 26.543 28 25 1
## 202009 19.000 19 19 0.000 25.000 25 25 0
## 202010 32.000 32 32 0.000 37.000 37 37 0
## 202011 21.229 22 20 0.646 28.771 30 28 1
## 202012 14.057 15 13 0.684 29.943 31 29 1
## 202013 6.353 16 0 4.760 44.206 50 35 5
## 202014 27.419 31 25 1.608 38.581 41 35 2
tail(bike, 5, 10)
## 日期 時間 場站代號 場站區域 場站名稱 經度 緯度 總停車格
## 202010 2015-01-12 12 1068 三重區 二重國中 25.06596 121.4805 70
## 202011 2015-01-12 12 1070 三重區 新北市立圖書館三重分館 25.06610 121.4955 50
## 202012 2015-01-12 12 1071 三重區 三張公園 25.07051 121.4909 44
## 202013 2015-01-12 12 70 信義區 捷運台北101/世貿站 25.03275 121.5616 52
## 202014 2015-01-12 12 121 中山區 龍江南京路口 25.05298 121.5406 66
## 平均車輛數 最大車輛數 最小車輛數 車輛數標準差 平均空位數 最大空位數 最小空位數 空位數標準差
## 202010 32.000 32 32 0.000 37.000 37 37 0
## 202011 21.229 22 20 0.646 28.771 30 28 1
## 202012 14.057 15 13 0.684 29.943 31 29 1
## 202013 6.353 16 0 4.760 44.206 50 35 5
## 202014 27.419 31 25 1.608 38.581 41 35 2
head(bike$日期)
## [1] 2014-12-08 2014-12-08 2014-12-08 2014-12-08 2014-12-08 2014-12-08
## 37 Levels: 0000-00-00 2014-12-08 2014-12-09 2014-12-10 2014-12-11 2014-12-12 ... 2015-01-12
#
str(bike)
## 'data.frame': 202014 obs. of 16 variables:
## $ 日期 : Factor w/ 37 levels "0000-00-00","2014-12-08",..: 2 2 2 2 2 2 2 2 2 2 ...
## $ 時間 : int 15 15 15 15 15 15 15 15 15 15 ...
## $ 場站代號 : int 2 3 4 5 6 7 8 9 10 11 ...
## $ 場站區域 : Factor w/ 20 levels "","北投區","大安區",..: 3 16 16 16 16 16 16 16 16 16 ...
## $ 場站名稱 : Factor w/ 258 levels "","八德市場",..: 65 182 172 230 169 223 170 176 181 162 ...
## $ 經度 : num 25 25 25 25 25 ...
## $ 緯度 : num 122 122 122 122 122 ...
## $ 總停車格 : int 48 40 60 60 80 80 60 40 54 66 ...
## $ 平均車輛數 : num 24 10.3 39.3 34.2 31.3 ...
## $ 最大車輛數 : int 27 13 40 35 32 25 20 24 17 46 ...
## $ 最小車輛數 : int 23 8 38 33 31 23 19 23 14 42 ...
## $ 車輛數標準差: num 1.549 1.862 1.033 0.753 0.516 ...
## $ 平均空位數 : num 22 29.7 20.7 25.8 47.7 ...
## $ 最大空位數 : int 23 32 22 27 48 57 41 17 40 23 ...
## $ 最小空位數 : int 19 27 20 25 47 55 40 16 37 19 ...
## $ 空位數標準差: int 2 2 1 1 1 1 1 1 2 1 ...
# ETL Start
# Time conversion
class(bike$日期)
## [1] "factor"
bike$日期 <- as.Date(bike$日期)
str(bike$日期)
## Date[1:202014], format: "2014-12-08" "2014-12-08" "2014-12-08" "2014-12-08" "2014-12-08" "2014-12-08" ...
# Create new variable
# ?strftime
# bike$星期 <- strftime(bike[, "日期"], format="%u")
# bike$已借 <- bike$總停車格 - bike$最大空位數
# bike$無車機率 <- round(bike$最大空位數 / bike$總停車格, digit = 3)
# bike$有車機率 <- 1 - bike$無車機率
bike <- mutate(bike,
星期 = strftime(日期, format="%u"),
已借 = 總停車格 - 最大空位數,
無車機率 = round(最大空位數 / 總停車格, digit = 3),
有車機率 = 1 - 無車機率
# 有車機率 = round(最大空位數 / 總停車格, digit = 3),
# 無車機率 = 1 - 有車機率
)
head(bike)
## 日期 時間 場站代號 場站區域 場站名稱 經度 緯度 總停車格 平均車輛數
## 1 2014-12-08 15 2 大安區 捷運國父紀念館站(2號出口) 25.04100 121.5569 48 24.000
## 2 2014-12-08 15 3 信義區 台北市政府 25.03780 121.5652 40 10.333
## 3 2014-12-08 15 4 信義區 市民廣場 25.03604 121.5623 60 39.333
## 4 2014-12-08 15 5 信義區 興雅國中 25.03656 121.5687 60 34.167
## 5 2014-12-08 15 6 信義區 世貿二館 25.03474 121.5657 80 31.333
## 6 2014-12-08 15 7 信義區 信義廣場(台北101) 25.03304 121.5656 80 23.571
## 最大車輛數 最小車輛數 車輛數標準差 平均空位數 最大空位數 最小空位數 空位數標準差 星期 已借
## 1 27 23 1.549 22.000 23 19 2 1 25
## 2 13 8 1.862 29.667 32 27 2 1 8
## 3 40 38 1.033 20.667 22 20 1 1 38
## 4 35 33 0.753 25.833 27 25 1 1 33
## 5 32 31 0.516 47.667 48 47 1 1 32
## 6 25 23 0.787 56.429 57 55 1 1 23
## 無車機率 有車機率
## 1 0.479 0.521
## 2 0.800 0.200
## 3 0.367 0.633
## 4 0.450 0.550
## 5 0.600 0.400
## 6 0.712 0.288
# Change the order of levels of a factor
# ?levels
bike$時間 <- as.factor(bike$時間)
levels(bike$時間)
## [1] "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13" "14" "15" "16" "17" "18"
## [20] "19" "20" "21" "22" "23"
bike$星期 <- as.factor(bike$星期)
levels(bike$星期)
## [1] "1" "2" "3" "4" "5" "6" "7"
levels(bike$星期) <- c(7,6,5,4,3,2,1)
str(bike$星期)
## Factor w/ 7 levels "7","6","5","4",..: 1 1 1 1 1 1 1 1 1 1 ...
levels(bike$星期) <- c(1,2,3,4,5,6,7)
# Number of levels
length(levels(bike$場站名稱))
## [1] 258
# bike$場站代號
# bike$日期 %Y, %m, %d
# Reference for exercise
# bike$場站代號
# bike$場站代號 <- as.factor(bike$場站代號)
# levels(bike$場站代號)
# bike$日期 %Y, %m, %d
# bike <- mutate(bike,
# 年 = strftime(日期, format="%Y"),
# 月 = strftime(日期, format="%m"),
# 日 = strftime(日期, format="%d")
# )
# head(bike)
# Abnormals
# Descriptive
summary(bike)
## 日期 時間 場站代號 場站區域 場站名稱
## Min. :2014-12-08 11 : 8513 Min. : 1.0 大安區 : 23997 福華商場: 833
## 1st Qu.:2014-12-17 0 : 8512 1st Qu.: 62.0 信義區 : 19129 裕隆公園: 833
## Median :2014-12-26 1 : 8512 Median : 123.0 中山區 : 16297 八德市場: 832
## Mean :2014-12-26 9 : 8512 Mean : 295.8 中正區 : 15295 白雲國小: 832
## 3rd Qu.:2015-01-04 10 : 8512 3rd Qu.: 185.0 松山區 : 12480 百齡國小: 832
## Max. :2015-01-12 8 : 8511 Max. :1071.0 內湖區 : 11642 (Other) :197125
## NA's :2 (Other):150942 (Other):103174 NA's : 727
## 經度 緯度 總停車格 平均車輛數 最大車輛數
## Min. : 0.00 Min. : 0.0 Min. : 20.0 Min. : 0.000 Min. : 0.00
## 1st Qu.:25.03 1st Qu.:121.5 1st Qu.: 36.0 1st Qu.: 5.767 1st Qu.: 9.00
## Median :25.05 Median :121.5 Median : 44.0 Median : 13.454 Median : 17.00
## Mean :25.05 Mean :121.5 Mean : 46.2 Mean : 15.458 Mean : 19.04
## 3rd Qu.:25.07 3rd Qu.:121.6 3rd Qu.: 52.0 3rd Qu.: 22.000 3rd Qu.: 26.00
## Max. :25.14 Max. :121.7 Max. :180.0 Max. :171.000 Max. :180.00
##
## 最小車輛數 車輛數標準差 平均空位數 最大空位數 最小空位數
## Min. : 0.00 Min. : 0.000 Min. : 0.00 Min. : 0.0 Min. : 0.00
## 1st Qu.: 2.00 1st Qu.: 0.711 1st Qu.: 19.90 1st Qu.: 23.0 1st Qu.: 16.00
## Median : 10.00 Median : 1.495 Median : 27.73 Median : 31.0 Median : 24.00
## Mean : 12.19 Mean : 2.212 Mean : 30.03 Mean : 33.3 Mean : 26.46
## 3rd Qu.: 19.00 3rd Qu.: 2.927 3rd Qu.: 37.39 3rd Qu.: 41.0 3rd Qu.: 34.00
## Max. :166.00 Max. :70.749 Max. :179.93 Max. :180.0 Max. :179.00
## NA's :11
## 空位數標準差 星期 已借 無車機率 有車機率
## Min. : 0.000 7 :29574 Min. : -2.0 Min. :0.0000 Min. :-0.0670
## 1st Qu.: 1.000 5 :29055 1st Qu.: 3.0 1st Qu.:0.5530 1st Qu.: 0.0590
## Median : 1.000 4 :29028 Median : 11.0 Median :0.7500 Median : 0.2500
## Mean : 2.201 3 :28896 Mean : 12.9 Mean :0.7248 Mean : 0.2752
## 3rd Qu.: 3.000 1 :28792 3rd Qu.: 20.0 3rd Qu.:0.9410 3rd Qu.: 0.4470
## Max. :70.000 (Other):56667 Max. :180.0 Max. :1.0670 Max. : 1.0000
## NA's :11 NA's : 2
# Adding new factor levels
head(filter(bike, is.na(場站名稱) == T))
## 日期 時間 場站代號 場站區域 場站名稱 經度 緯度 總停車格 平均車輛數 最大車輛數
## 1 2014-12-13 0 185 大安區 <NA> 25.04234 121.546 30 0.000 0
## 2 2014-12-13 1 185 大安區 <NA> 25.04234 121.546 30 3.000 13
## 3 2014-12-13 2 185 大安區 <NA> 25.04234 121.546 30 10.133 12
## 4 2014-12-13 3 185 大安區 <NA> 25.04234 121.546 30 6.733 8
## 5 2014-12-13 4 185 大安區 <NA> 25.04234 121.546 30 5.000 5
## 6 2014-12-13 5 185 大安區 <NA> 25.04234 121.546 30 1.400 5
## 最小車輛數 車輛數標準差 平均空位數 最大空位數 最小空位數 空位數標準差 星期 已借 無車機率 有車機率
## 1 0 0.000 0.000 0 0 0 6 30 0.000 1.000
## 2 0 5.398 9.667 28 0 11 6 2 0.933 0.067
## 3 8 1.727 18.867 21 17 2 6 9 0.700 0.300
## 4 5 1.280 22.267 24 21 1 6 6 0.800 0.200
## 5 5 0.000 24.000 24 24 0 6 6 0.800 0.200
## 6 0 2.063 27.600 29 24 2 6 1 0.967 0.033
distinct(select(filter(bike, is.na(場站名稱) == T), 場站名稱))
## 場站名稱
## 1 <NA>
# unique(filter(bike, is.na(場站名稱) == T)$場站代號)
distinct(select(filter(bike, is.na(場站名稱) == T), 場站代號))
## 場站代號
## 1 185
# http://taipei.youbike.com.tw/cht/f11.php
bike$場站名稱 <- as.character(bike$場站名稱)
bike[which(is.na(bike$場站名稱) == T),"場站名稱"] <- "瑠公公園"
bike$場站名稱 <- as.factor(bike$場站名稱)
# Wire porps.
summary(bike)
## 日期 時間 場站代號 場站區域 場站名稱
## Min. :2014-12-08 11 : 8513 Min. : 1.0 大安區 : 23997 福華商場 : 833
## 1st Qu.:2014-12-17 0 : 8512 1st Qu.: 62.0 信義區 : 19129 裕隆公園 : 833
## Median :2014-12-26 1 : 8512 Median : 123.0 中山區 : 16297 八德市場 : 832
## Mean :2014-12-26 9 : 8512 Mean : 295.8 中正區 : 15295 白雲國小 : 832
## 3rd Qu.:2015-01-04 10 : 8512 3rd Qu.: 185.0 松山區 : 12480 百齡國小 : 832
## Max. :2015-01-12 8 : 8511 Max. :1071.0 內湖區 : 11642 保安永平路口: 832
## NA's :2 (Other):150942 (Other):103174 (Other) :197020
## 經度 緯度 總停車格 平均車輛數 最大車輛數
## Min. : 0.00 Min. : 0.0 Min. : 20.0 Min. : 0.000 Min. : 0.00
## 1st Qu.:25.03 1st Qu.:121.5 1st Qu.: 36.0 1st Qu.: 5.767 1st Qu.: 9.00
## Median :25.05 Median :121.5 Median : 44.0 Median : 13.454 Median : 17.00
## Mean :25.05 Mean :121.5 Mean : 46.2 Mean : 15.458 Mean : 19.04
## 3rd Qu.:25.07 3rd Qu.:121.6 3rd Qu.: 52.0 3rd Qu.: 22.000 3rd Qu.: 26.00
## Max. :25.14 Max. :121.7 Max. :180.0 Max. :171.000 Max. :180.00
##
## 最小車輛數 車輛數標準差 平均空位數 最大空位數 最小空位數
## Min. : 0.00 Min. : 0.000 Min. : 0.00 Min. : 0.0 Min. : 0.00
## 1st Qu.: 2.00 1st Qu.: 0.711 1st Qu.: 19.90 1st Qu.: 23.0 1st Qu.: 16.00
## Median : 10.00 Median : 1.495 Median : 27.73 Median : 31.0 Median : 24.00
## Mean : 12.19 Mean : 2.212 Mean : 30.03 Mean : 33.3 Mean : 26.46
## 3rd Qu.: 19.00 3rd Qu.: 2.927 3rd Qu.: 37.39 3rd Qu.: 41.0 3rd Qu.: 34.00
## Max. :166.00 Max. :70.749 Max. :179.93 Max. :180.0 Max. :179.00
## NA's :11
## 空位數標準差 星期 已借 無車機率 有車機率
## Min. : 0.000 7 :29574 Min. : -2.0 Min. :0.0000 Min. :-0.0670
## 1st Qu.: 1.000 5 :29055 1st Qu.: 3.0 1st Qu.:0.5530 1st Qu.: 0.0590
## Median : 1.000 4 :29028 Median : 11.0 Median :0.7500 Median : 0.2500
## Mean : 2.201 3 :28896 Mean : 12.9 Mean :0.7248 Mean : 0.2752
## 3rd Qu.: 3.000 1 :28792 3rd Qu.: 20.0 3rd Qu.:0.9410 3rd Qu.: 0.4470
## Max. :70.000 (Other):56667 Max. :180.0 Max. :1.0670 Max. : 1.0000
## NA's :11 NA's : 2
filter(bike, 已借 < 0)
## 日期 時間 場站代號 場站區域 場站名稱 經度 緯度 總停車格 平均車輛數
## 1 2015-01-11 8 25 信義區 永吉松信路口 25.04543 121.5721 30 0.889
## 2 2015-01-11 21 45 大安區 捷運公館站(2號出口) 25.01476 121.5345 30 3.095
## 最大車輛數 最小車輛數 車輛數標準差 平均空位數 最大空位數 最小空位數 空位數標準差 星期 已借
## 1 3 0 1.071 29.511 32 27 2 7 -2
## 2 9 0 2.467 26.405 31 21 3 7 -1
## 無車機率 有車機率
## 1 1.067 -0.067
## 2 1.033 -0.033
filter(bike, 有車機率 > 1)
## [1] 日期 時間 場站代號 場站區域 場站名稱 經度 緯度
## [8] 總停車格 平均車輛數 最大車輛數 最小車輛數 車輛數標準差 平均空位數 最大空位數
## [15] 最小空位數 空位數標準差 星期 已借 無車機率 有車機率
## <0 rows> (or 0-length row.names)
bike <- filter(bike, 已借 >= 0)
# Blank
filter(bike, 場站名稱 == "")
## 日期 時間 場站代號 場站區域 場站名稱 經度 緯度 總停車格 平均車輛數 最大車輛數 最小車輛數
## 1 2014-12-29 10 1071 0 0 20 0 0 0
## 車輛數標準差 平均空位數 最大空位數 最小空位數 空位數標準差 星期 已借 無車機率 有車機率
## 1 NA 0 0 0 NA 1 20 0 1
bike <- filter(bike, 場站名稱 != "")
# Remove empty level
levels(bike$場站區域)
## [1] "" "北投區" "大安區" "大同區" "蘆洲區" "南港區" "內湖區" "三重區" "士林區" "松山區"
## [11] "萬華區" "文山區" "汐止區" "新店區" "新莊區" "信義區" "永和區" "中和區" "中山區" "中正區"
bike$場站區域 <- factor(bike$場站區域)
table(factor(bike$場站區域))
##
## 北投區 大安區 大同區 蘆洲區 南港區 內湖區 三重區 士林區 松山區 萬華區 文山區 汐止區 新店區 新莊區
## 9980 23996 8854 2496 10260 11642 6460 11124 12480 9981 9865 9547 9248 7040
## 信義區 永和區 中和區 中山區 中正區
## 19128 5822 2496 16297 15295
# Save & Load dataset
# save(bike, file = "ana_bike2.RDA")
# load("ana_bike.RDA")
- qplot() - for quick plots - Getting start with qplot
qplot(x, y, data=, color=, shape=, size=, alpha=,
geom=, method=, formula=, facets=,
xlim=, ylim= xlab=, ylab=, main=, sub=)
ggplot(data = , aes( x = , y = ), fill = , colour = ) +
geom_() +
theme_() +
coord_() + facet_() + stat_ () + …
library(ggplot2)
# list all geom
ls(pattern = '^geom_', env = as.environment('package:ggplot2'))
## [1] "geom_abline" "geom_area" "geom_bar" "geom_bin2d" "geom_blank"
## [6] "geom_boxplot" "geom_contour" "geom_crossbar" "geom_density" "geom_density2d"
## [11] "geom_dotplot" "geom_errorbar" "geom_errorbarh" "geom_freqpoly" "geom_hex"
## [16] "geom_histogram" "geom_hline" "geom_jitter" "geom_line" "geom_linerange"
## [21] "geom_map" "geom_path" "geom_point" "geom_pointrange" "geom_polygon"
## [26] "geom_quantile" "geom_raster" "geom_rect" "geom_ribbon" "geom_rug"
## [31] "geom_segment" "geom_smooth" "geom_step" "geom_text" "geom_tile"
## [36] "geom_violin" "geom_vline"
ls(pattern = '^stat_', env = as.environment('package:ggplot2'))
## [1] "stat_abline" "stat_bin" "stat_bin2d" "stat_bindot" "stat_binhex"
## [6] "stat_boxplot" "stat_contour" "stat_density" "stat_density2d" "stat_ecdf"
## [11] "stat_ellipse" "stat_function" "stat_hline" "stat_identity" "stat_qq"
## [16] "stat_quantile" "stat_smooth" "stat_spoke" "stat_sum" "stat_summary"
## [21] "stat_summary_hex" "stat_summary2d" "stat_unique" "stat_vline" "stat_ydensity"
# EDA Start
summary(bike)
## 日期 時間 場站代號 場站區域 場站名稱
## Min. :2014-12-08 11 : 8513 Min. : 1.0 大安區 : 23996 福華商場 : 833
## 1st Qu.:2014-12-17 0 : 8512 1st Qu.: 62.0 信義區 : 19128 裕隆公園 : 833
## Median :2014-12-26 1 : 8512 Median : 123.0 中山區 : 16297 八德市場 : 832
## Mean :2014-12-26 9 : 8512 Mean : 295.8 中正區 : 15295 白雲國小 : 832
## 3rd Qu.:2015-01-04 10 : 8511 3rd Qu.: 185.0 松山區 : 12480 百齡國小 : 832
## Max. :2015-01-12 2 : 8510 Max. :1071.0 內湖區 : 11642 保安永平路口: 832
## NA's :2 (Other):150941 (Other):103173 (Other) :197017
## 經度 緯度 總停車格 平均車輛數 最大車輛數
## Min. :24.97 Min. :121.4 Min. : 24.0 Min. : 0.000 Min. : 0.00
## 1st Qu.:25.03 1st Qu.:121.5 1st Qu.: 36.0 1st Qu.: 5.767 1st Qu.: 9.00
## Median :25.05 Median :121.5 Median : 44.0 Median : 13.455 Median : 17.00
## Mean :25.05 Mean :121.5 Mean : 46.2 Mean : 15.458 Mean : 19.04
## 3rd Qu.:25.07 3rd Qu.:121.6 3rd Qu.: 52.0 3rd Qu.: 22.000 3rd Qu.: 26.00
## Max. :25.14 Max. :121.7 Max. :180.0 Max. :171.000 Max. :180.00
##
## 最小車輛數 車輛數標準差 平均空位數 最大空位數 最小空位數
## Min. : 0.00 Min. : 0.000 Min. : 0.00 Min. : 0.0 Min. : 0.00
## 1st Qu.: 2.00 1st Qu.: 0.711 1st Qu.: 19.90 1st Qu.: 23.0 1st Qu.: 16.00
## Median : 10.00 Median : 1.495 Median : 27.73 Median : 31.0 Median : 24.00
## Mean : 12.19 Mean : 2.212 Mean : 30.03 Mean : 33.3 Mean : 26.46
## 3rd Qu.: 19.00 3rd Qu.: 2.927 3rd Qu.: 37.39 3rd Qu.: 41.0 3rd Qu.: 34.00
## Max. :166.00 Max. :70.749 Max. :179.93 Max. :180.0 Max. :179.00
## NA's :10
## 空位數標準差 星期 已借 無車機率 有車機率
## Min. : 0.000 7 :29572 Min. : 0.0 Min. :0.0000 Min. :0.0000
## 1st Qu.: 1.000 5 :29055 1st Qu.: 3.0 1st Qu.:0.5530 1st Qu.:0.0590
## Median : 1.000 4 :29028 Median : 11.0 Median :0.7500 Median :0.2500
## Mean : 2.201 3 :28896 Mean : 12.9 Mean :0.7248 Mean :0.2752
## 3rd Qu.: 3.000 1 :28791 3rd Qu.: 20.0 3rd Qu.:0.9410 3rd Qu.:0.4470
## Max. :70.000 (Other):56667 Max. :180.0 Max. :1.0000 Max. :1.0000
## NA's :10 NA's : 2
# Aggregated hourly data analysis
# R build-in plot
par(family = "STHeiti", mfrow = c(1,1)) # Mac 中文字型設定
hist(bike$無車機率, breaks = seq(0,1,0.1))
# qplot
qplot(data = bike, 無車機率, geom = "auto", binwidth = .1)
filter(bike, 無車機率 > 1)
## [1] 日期 時間 場站代號 場站區域 場站名稱 經度 緯度
## [8] 總停車格 平均車輛數 最大車輛數 最小車輛數 車輛數標準差 平均空位數 最大空位數
## [15] 最小空位數 空位數標準差 星期 已借 無車機率 有車機率
## <0 rows> (or 0-length row.names)
# qplot(data = bike, 無車機率, geom = "auto", binwidth = .1, xlim = c(0,1))
# ggplot
bike.1p <- ggplot(bike, aes(x = 無車機率)) + theme_gray(base_family="STHeiti")
bike.1p + geom_histogram(binwidth = .1)
bike.1p + geom_histogram(binwidth = .1, aes(fill = ..count..))
bike.1p + geom_histogram(binwidth = .1, aes(fill = ..count..)) + xlim(0,1)
bike.1p + geom_histogram(binwidth = .1, aes(fill = ..count..)) + xlim(0,1) + scale_y_reverse()
## Warning: Stacking not well defined when ymin != 0
# density
bike.1p + geom_histogram(binwidth = .1, aes(fill = ..count..)) + xlim(0,1) +
geom_density(aes(y=..count..))
qplot(data = bike, 無車機率, geom = "density", binwidth = .1)
# think!
dim(filter(bike, 有車機率 > 0))[1] / dim(bike)[1]
## [1] 0.8750118
# R build-in plot
boxplot(bike$有車機率)
boxplot(bike$有車機率, horizontal = T)
# ggplot
bike.2p <- ggplot(bike, aes(x = 場站區域, y = 有車機率)) + theme_gray(base_family="STHeiti")
bike.2p + geom_boxplot() + coord_flip()
# Put Average on Boxplot
means <- aggregate(有車機率 ~ 場站區域, bike, mean)
means$有車機率 <- round(means$有車機率, digit=2)
bike.2p + geom_boxplot() + coord_flip() +
stat_summary(fun.y=mean, colour="darkred", geom="point",
shape=18, size=3,show_guide = FALSE) +
geom_text(data = means, aes(label = 有車機率, y = 有車機率 + 0.08))
# 看各區分佈借車機率圖
# Subset
bike.sub <- filter(bike, 日期 == "2014-12-09" & 時間 == 15)
bike.sub.s <- summarise(group_by(bike.sub, 場站區域), n_distinct(場站名稱), sum(總停車格), n_distinct(星期))
names(bike.sub.s) <- c("場站區域", "場站數", "總停車格", "星期")
bike.sub.s
## Source: local data frame [19 x 4]
##
## 場站區域 場站數 總停車格 星期
## 1 北投區 12 612 1
## 2 大安區 27 1298 1
## 3 大同區 10 394 1
## 4 蘆洲區 3 186 1
## 5 南港區 11 382 1
## 6 內湖區 11 452 1
## 7 三重區 5 248 1
## 8 士林區 12 520 1
## 9 松山區 15 688 1
## 10 萬華區 12 488 1
## 11 文山區 11 512 1
## 12 汐止區 9 450 1
## 13 新店區 7 290 1
## 14 新莊區 7 312 1
## 15 信義區 23 1266 1
## 16 永和區 7 338 1
## 17 中和區 3 180 1
## 18 中山區 19 846 1
## 19 中正區 17 774 1
names(bike.sub.s) <- c("District", "Stations", "Numbers", "Weekday")
# R build-in plot
par(family="Microsoft JhengHei")
barplot(bike.sub.s$Stations,
names.arg = bike.sub.s$District)
# reorder: why?
bike.sub.s <- bike.sub.s[order(bike.sub.s$Stations, decreasing = T),]
barplot(bike.sub.s$Stations,
names.arg = bike.sub.s$District, las = 2, ylim = c(0,30))
# ggplot
bike.3p <- ggplot(data = bike.sub.s, aes(x = reorder(District, -Stations), y = Stations,
colour = District, fill = District))
bike.3p + geom_bar(stat="identity", width=0.5)
bike.3p + geom_bar(stat="identity", width=0.5) +
theme_gray(base_family="Microsoft JhengHei") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
bike.3p + geom_bar(stat="identity", width=0.5) +
theme_gray(base_family="Microsoft JhengHei") +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
coord_flip()
# Polar chart
bike.3p + geom_bar(stat="identity", width=0.5) +
theme_gray(base_family="Microsoft JhengHei") +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
coord_polar()
bike.3p + geom_bar(stat="identity", width=0.5) +
theme_gray(base_family="Microsoft JhengHei") +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
coord_polar(theta = "y")
# PIE chart
# group by column
# 各區場站數佔比
ggplot(data = bike.sub.s, aes(x = Weekday, y = Stations,
colour = District, fill = District, group = Weekday)) +
geom_bar(stat="identity", width=1, position = "stack") +
theme_gray(base_family="Microsoft JhengHei") +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
coord_polar(theta = "y")
# position = "stack" / "dodge" ?
# Subset II
bike.sub.m <- melt(select(bike.sub, 場站區域, 場站名稱, 有車機率, 無車機率))
## Using 場站區域, 場站名稱 as id variables
filter(bike.sub.m, 場站區域 == "大安區")
## 場站區域 場站名稱 variable value
## 1 大安區 捷運國父紀念館站(2號出口) 有車機率 0.542
## 2 大安區 捷運科技大樓站 有車機率 0.107
## 3 大安區 信義建國路口 有車機率 0.543
## 4 大安區 金山愛國路口 有車機率 0.333
## 5 大安區 基隆長興路口 有車機率 0.284
## 6 大安區 辛亥新生路口 有車機率 0.233
## 7 大安區 捷運六張犁站 有車機率 0.600
## 8 大安區 臺大資訊大樓 有車機率 0.722
## 9 大安區 捷運東門站(4號出口) 有車機率 0.261
## 10 大安區 臺灣師範大學(圖書館) 有車機率 0.500
## 11 大安區 捷運公館站(2號出口) 有車機率 0.033
## 12 大安區 捷運忠孝新生(3號出口) 有車機率 0.375
## 13 大安區 龍門廣場 有車機率 0.731
## 14 大安區 臺北市立圖書館(總館) 有車機率 0.600
## 15 大安區 新生和平路口 有車機率 0.283
## 16 大安區 台灣科技大學 有車機率 0.109
## 17 大安區 仁愛醫院 有車機率 0.361
## 18 大安區 捷運信義安和站 有車機率 0.100
## 19 大安區 捷運台電大樓站(2號出口) 有車機率 0.175
## 20 大安區 捷運大安森林公園站 有車機率 0.716
## 21 大安區 信義敦化路口 有車機率 0.783
## 22 大安區 捷運大安站 有車機率 0.707
## 23 大安區 捷運忠孝復興站(2號出口) 有車機率 0.852
## 24 大安區 成功國宅 有車機率 0.694
## 25 大安區 羅斯福新生南路口 有車機率 0.125
## 26 大安區 捷運麟光站2號出口 有車機率 0.653
## 27 大安區 敦化基隆路口 有車機率 0.267
## 28 大安區 捷運國父紀念館站(2號出口) 無車機率 0.458
## 29 大安區 捷運科技大樓站 無車機率 0.893
## 30 大安區 信義建國路口 無車機率 0.457
## 31 大安區 金山愛國路口 無車機率 0.667
## 32 大安區 基隆長興路口 無車機率 0.716
## 33 大安區 辛亥新生路口 無車機率 0.767
## 34 大安區 捷運六張犁站 無車機率 0.400
## 35 大安區 臺大資訊大樓 無車機率 0.278
## 36 大安區 捷運東門站(4號出口) 無車機率 0.739
## 37 大安區 臺灣師範大學(圖書館) 無車機率 0.500
## 38 大安區 捷運公館站(2號出口) 無車機率 0.967
## 39 大安區 捷運忠孝新生(3號出口) 無車機率 0.625
## 40 大安區 龍門廣場 無車機率 0.269
## 41 大安區 臺北市立圖書館(總館) 無車機率 0.400
## 42 大安區 新生和平路口 無車機率 0.717
## 43 大安區 台灣科技大學 無車機率 0.891
## 44 大安區 仁愛醫院 無車機率 0.639
## 45 大安區 捷運信義安和站 無車機率 0.900
## 46 大安區 捷運台電大樓站(2號出口) 無車機率 0.825
## 47 大安區 捷運大安森林公園站 無車機率 0.284
## 48 大安區 信義敦化路口 無車機率 0.217
## 49 大安區 捷運大安站 無車機率 0.293
## 50 大安區 捷運忠孝復興站(2號出口) 無車機率 0.148
## 51 大安區 成功國宅 無車機率 0.306
## 52 大安區 羅斯福新生南路口 無車機率 0.875
## 53 大安區 捷運麟光站2號出口 無車機率 0.347
## 54 大安區 敦化基隆路口 無車機率 0.733
ggplot(filter(bike.sub.m, 場站區域 == "大安區"),
aes(x = 場站名稱, y = value, fill = variable, width =.5)) +
geom_bar(stat="identity", position = "dodge", width = .5) +
theme_gray(base_family="Microsoft JhengHei")
# Stacked bar chart: 重要的放上面還是下面?
ggplot(filter(bike.sub.m, 場站區域 == "大安區"),
aes(x = 場站名稱, y = value, fill = variable, width =.5)) +
geom_bar(stat="identity", position = "stack", width = .5) +
theme_gray(base_family="Microsoft JhengHei")
# Your code here
# recall
bike.sub <- filter(bike, 日期 == "2014-12-09" & 時間 == 15)
bike.sub2 <- filter(bike, 星期 == 1 & 時間 == 15)
summary(bike.sub2)
## 日期 時間 場站代號 場站區域 場站名稱
## Min. :2014-12-08 15 :1201 Min. : 1.0 大安區 :144 八德市場 : 5
## 1st Qu.:2014-12-15 0 : 0 1st Qu.: 61.0 信義區 :115 白雲國小 : 5
## Median :2014-12-22 1 : 0 Median : 121.0 中山區 : 98 百齡國小 : 5
## Mean :2014-12-22 2 : 0 Mean : 290.3 中正區 : 91 保安永平路口: 5
## 3rd Qu.:2014-12-29 3 : 0 3rd Qu.: 182.0 松山區 : 75 北投運動中心: 5
## Max. :2015-01-05 4 : 0 Max. :1071.0 內湖區 : 69 成功國宅 : 5
## (Other): 0 (Other):609 (Other) :1171
## 經度 緯度 總停車格 平均車輛數 最大車輛數 最小車輛數
## Min. :24.97 Min. :121.4 Min. : 24.00 Min. : 0.111 Min. : 1.00 Min. : 0.0
## 1st Qu.:25.03 1st Qu.:121.5 1st Qu.: 36.00 1st Qu.: 5.767 1st Qu.: 9.00 1st Qu.: 2.0
## Median :25.05 Median :121.5 Median : 44.00 Median :12.667 Median :17.00 Median : 9.0
## Mean :25.05 Mean :121.5 Mean : 46.19 Mean :15.090 Mean :18.96 Mean :11.7
## 3rd Qu.:25.07 3rd Qu.:121.6 3rd Qu.: 52.00 3rd Qu.:21.311 3rd Qu.:26.00 3rd Qu.:18.0
## Max. :25.14 Max. :121.7 Max. :180.00 Max. :96.000 Max. :98.00 Max. :93.0
##
## 車輛數標準差 平均空位數 最大空位數 最小空位數 空位數標準差 星期
## Min. : 0.000 Min. : 2.067 Min. : 3.00 Min. : 0.00 Min. : 0.000 1:1201
## 1st Qu.: 0.971 1st Qu.: 21.333 1st Qu.: 24.00 1st Qu.: 17.00 1st Qu.: 1.000 2: 0
## Median : 1.704 Median : 28.422 Median : 32.00 Median : 25.00 Median : 2.000 3: 0
## Mean : 2.298 Mean : 30.472 Mean : 33.87 Mean : 26.61 Mean : 2.324 4: 0
## 3rd Qu.: 2.914 3rd Qu.: 37.167 3rd Qu.: 41.00 3rd Qu.: 34.00 3rd Qu.: 3.000 5: 0
## Max. :14.822 Max. :168.967 Max. :180.00 Max. :153.00 Max. :15.000 6: 0
## 7: 0
## 已借 無車機率 有車機率
## Min. : 0.00 Min. :0.1150 Min. :0.0000
## 1st Qu.: 2.00 1st Qu.:0.5770 1st Qu.:0.0590
## Median :10.00 Median :0.7750 Median :0.2250
## Mean :12.32 Mean :0.7405 Mean :0.2595
## 3rd Qu.:19.00 3rd Qu.:0.9410 3rd Qu.:0.4230
## Max. :94.00 Max. :1.0000 Max. :0.8850
##
bike.sub2.s <- summarise(group_by(bike.sub2, 場站區域, 場站名稱, 總停車格), mean(有車機率))names(bike.sub2.s)[4] <- "週一平均有車機率"
# R build-in X-Y plot
# ?plot()
plot(bike.sub2.s$總停車格, bike.sub2.s$週一平均有車機率, pch = 20, cex = 1)
plot(bike.sub2.s$總停車格, bike.sub2.s$週一平均有車機率, pch = 20, cex = 1, col = rainbow(20))
# ggplot
bike.4p <- ggplot(data = bike.sub2.s, aes(x = 總停車格, y = 週一平均有車機率,colour = 場站區域)) +
theme_gray(base_family="Microsoft JhengHei")
bike.4p + geom_point()
bike.4p + geom_point(aes(size = 總停車格, alpha = 1/20)) + scale_size_continuous(range = c(5,10))
# Point with Text plot
bike.4p + geom_point() +
geom_text(aes(label = 場站名稱, size = 總停車格), hjust = .5, vjust = -.8, family="Microsoft JhengHei") +
scale_size_continuous(range=c(3,8))
bike.sub3.s <- summarise(group_by(filter(bike, 時間 == 15 & 場站區域 == "信義區"),
星期, 場站區域, 場站名稱, 總停車格), mean(有車機率))
names(bike.sub3.s)[5] <- "有車機率"
# Define new variables
summary(bike.sub3.s)
## 星期 場站區域 場站名稱 總停車格 有車機率
## 1:23 信義區 :161 福德公園 : 7 Min. : 30.00 Min. :0.0066
## 2:23 北投區 : 0 基隆光復路口 : 7 1st Qu.: 36.00 1st Qu.:0.1334
## 3:23 大安區 : 0 捷運市政府站(3號出口): 7 Median : 52.00 Median :0.2108
## 4:23 大同區 : 0 捷運台北101/世貿站 : 7 Mean : 55.04 Mean :0.2291
## 5:23 蘆洲區 : 0 捷運象山站 : 7 3rd Qu.: 60.00 3rd Qu.:0.3268
## 6:23 南港區 : 0 捷運永春站(2號出口) : 7 Max. :180.00 Max. :0.5768
## 7:23 (Other): 0 (Other) :119
bike.sub3.s <- mutate(bike.sub3.s, Big = (總停車格 > 60), Easy = (有車機率 > 0.5))
# Grid by weekday
ggplot(data = bike.sub3.s, aes(x = 總停車格, y = 有車機率, colour = 場站名稱)) +
theme_gray(base_family="Microsoft JhengHei") +
geom_point(aes(size = 1)) +
geom_text(aes(label = 場站名稱, size = 1), hjust = .5, vjust = -.8, family="Microsoft JhengHei") +
facet_grid(.~星期)
# Change the factor order
str(bike.sub3.s$Easy)
## logi [1:161] FALSE FALSE FALSE FALSE FALSE FALSE ...
bike.sub3.s$Easy <- as.factor(bike.sub3.s$Easy)
levels(bike.sub3.s$Easy)
## [1] "FALSE" "TRUE"
levels(bike.sub3.s$Easy) <- c("HARD", "EASY")
bike.sub3.s$Easy <- factor(bike.sub3.s$Easy, levels=rev(levels(bike.sub3.s$Easy)) )
# Grid by easy and weekday
ggplot(data = bike.sub3.s, aes(x = 總停車格, y = 有車機率, colour = 場站名稱)) +
theme_gray(base_family="Microsoft JhengHei") +
geom_point(aes(size = 1)) +
geom_text(aes(label = 場站名稱, size = 1), hjust = .5, vjust = -.8, family="Microsoft JhengHei") +
facet_grid(Easy~星期)
bike.sub4.s <- summarise(group_by(filter(bike, 場站區域 == "信義區"), 日期, 時間, 場站區域, 場站名稱, 總停車格),
mean(有車機率))names(bike.sub4.s)[6] <- "有車機率"
head(bike.sub4.s, 10)
## Source: local data frame [10 x 6]
## Groups: 日期, 時間, 場站區域, 場站名稱
##
## 日期 時間 場站區域 場站名稱 總停車格 有車機率
## 1 2014-12-08 15 信義區 福德公園 66 0.758
## 2 2014-12-08 15 信義區 基隆光復路口 30 0.533
## 3 2014-12-08 15 信義區 捷運市政府站(3號出口) 180 0.522
## 4 2014-12-08 15 信義區 捷運台北101/世貿站 52 0.135
## 5 2014-12-08 15 信義區 捷運象山站 58 0.207
## 6 2014-12-08 15 信義區 捷運永春站(2號出口) 30 0.200
## 7 2014-12-08 15 信義區 仁愛逸仙路口 38 0.263
## 8 2014-12-08 15 信義區 三張犁 66 0.652
## 9 2014-12-08 15 信義區 世貿二館 80 0.400
## 10 2014-12-08 15 信義區 世貿三館 60 0.317
bike.sub4.s <- as.data.frame(bike.sub4.s)
# ?geom_line
ggplot(data = bike.sub4.s, aes(x = 時間, y = 有車機率, colour = 場站名稱, group = 場站名稱)) +
theme_gray(base_family="Microsoft JhengHei") +
geom_line()
# ?geom_tile
ggplot(data = bike.sub4.s, aes(x = 時間, y = 場站名稱)) +
theme_gray(base_family="Microsoft JhengHei") +
geom_tile(aes(fill = 有車機率)) +
scale_fill_gradient(low="white", high="steelblue") +
scale_x_discrete(breaks=seq(0,23,1)) +
theme(axis.text.x = element_text(size = 13, color="darkred")) # + geom_vline(xintercept = 9.5)
bike.sub5.s <- summarise(
group_by(filter(bike, 場站區域 == "信義區"), 日期, 星期, 時間, 場站區域, 場站名稱, 總停車格),
mean(有車機率))
names(bike.sub5.s)[7] <- "有車機率"
bike.sub5.s <- mutate(bike.sub5.s, Workday = (星期 %in% 1:5))
head(bike.sub5.s)
## Source: local data frame [6 x 8]
## Groups: 日期, 星期, 時間, 場站區域, 場站名稱
##
## 日期 星期 時間 場站區域 場站名稱 總停車格 有車機率 Workday
## 1 2014-12-08 1 15 信義區 福德公園 66 0.758 TRUE
## 2 2014-12-08 1 15 信義區 基隆光復路口 30 0.533 TRUE
## 3 2014-12-08 1 15 信義區 捷運市政府站(3號出口) 180 0.522 TRUE
## 4 2014-12-08 1 15 信義區 捷運台北101/世貿站 52 0.135 TRUE
## 5 2014-12-08 1 15 信義區 捷運象山站 58 0.207 TRUE
## 6 2014-12-08 1 15 信義區 捷運永春站(2號出口) 30 0.200 TRUE
class(bike.sub5.s$Workday)
## [1] "logical"
# Change the factor order
bike.sub5.s$Workday <- as.factor(bike.sub5.s$Workday)
levels(bike.sub5.s$Workday) <- c("WEEKEND", "WORKDAY")
bike.sub5.s$Workday <- factor(bike.sub5.s$Workday, levels=rev(levels(bike.sub5.s$Workday)) )
# Color matters
# ?geom_tile
ggplot(data = bike.sub5.s, aes(x = 時間, y = 場站名稱)) +
theme_gray(base_family="Microsoft JhengHei") +
geom_tile(aes(fill = 有車機率)) +
scale_fill_gradient(low="red", high="white") +
scale_x_discrete(breaks=seq(0,23,1)) +
theme(axis.text.x = element_text(size = 13, color="darkred")) +
facet_grid(Workday~.)
# ?geom_tile
ggplot(data = bike.sub5.s, aes(x = 時間, y = 場站名稱)) +
theme_gray(base_family="Microsoft JhengHei") +
geom_tile(aes(fill = 有車機率)) +
scale_fill_gradient(low="white", high="midnightblue") +
scale_x_discrete(breaks=seq(0,23,1)) +
theme(axis.text.x = element_text(size = 13, color="darkred")) +
facet_grid(Workday~.)
# Your code here
Gook Luck!
Rafe C. H. Liu
Contact me| View Liu Chia Hsin’s profile
Data Analyst Training Program @ DSP